<template>
	<view class="register">
		<view class="bg">
			<view class="big-box">
				<view class="logo">
					<image src="../../static/logo.png" mode="widthFix"></image>
				</view>
				<view class="title">
					轻 阅
				</view>
				<view class="list">
					<text>昵称：</text>
					<input type="text" placeholder="请输入您的昵称" placeholder-style="color: #9c9c9c"
						v-model="userInfo.nickName">
				</view>
				<view class="list">
					<text>账号：</text>
					<input type="text" placeholder="请输入您的账号" placeholder-style="color: #9c9c9c"
						v-model="userInfo.phone">
				</view>
				<view class="list">
					<text>密码：</text>
					<input type="password" placeholder="请输入您的密码" placeholder-style="color: #9c9c9c"
						v-model="userInfo.password">
				</view>

				<view class="btn-box">
					<view class="btn" @click="goLogin()">
						返回登录
					</view>
					<view class="btn" @click="addUser()">
						注册用户
					</view>
				</view>

			</view>
		</view>
		<view class="bottom">
			
		</view>
	</view>
</template>

<script>
	export default {
		name: 'register',
		data() {
			return {
				userInfo: {
					nickName: '',
					phone: '',
					password: '',
					bookstore: [],
				}
			};
		},
		methods: {
			goLogin() {
				uni.navigateTo({
					url: '../login/login'
				})
			},
			async addUser() {
				if (this.userInfo.nickName.length == 0 || this.userInfo.phone.length == 0 || this.userInfo.password
					.length == 0) {
					uni.showToast({
						title: '昵称,账号,密码不能为空',
						icon: 'none'
					})
					return
				} else {
					await uniCloud.callFunction({
						name: 'getUser',
						data: {
							phone: this.userInfo.phone
						}
					}).then(res => {
						console.log('res ==> ', res);

						if (res.result.data.length == 0) {
							uniCloud.callFunction({
								name: 'addUser',
								data: {
									...this.userInfo
								}
							}).then(res => {
								console.log('res ==> ', res);
								uni.showToast({
									title: '注册成功',
									icon: 'none'
								})
								setTimeout(() => {
									uni.navigateTo({
										url: '../login/login'
									})
								}, 2000)

							}).catch(err => {
								console.log('err ==> ', err);
							})
						} else {
							uni.showToast({
								title: '用户已存在',
								icon: 'none'
							})
						}

					}).catch(err => {
						console.log('err ==> ', err);
					})
				}


			},
		}
	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		// height: 1234rpx;
		height: 100vh;
		background-image: url('../../static/background-image.png');
		background-repeat: no-repeat;
	}
	.bottom{
		width: 100%;
		height: 80rpx;
		background-color: #FFE5B4;
	}

	.big-box {
		width: 680rpx;
		height: 1100rpx;
		background-color: #FCECCC;
		margin: 0 auto;
		position: absolute;
		top: 60px;
		left: 18px;
		border-radius: 40rpx;
		border: 1px solid #FC768A;

		.logo {
			width: 160rpx;
			height: 160rpx;
			margin: 0 auto;
			margin-top: 60rpx;

			image {
				width: 100%;
				display: block;
			}
		}

		.title {
			text-align: center;
			font-size: 60rpx;
			margin-top: 20rpx;
			color: #FCB271;
		}

		.list {
			display: flex;
			align-items: center;
			width: 600rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-bottom: 1px solid #f5841a;
			margin: 50rpx auto;
		}

		.btn-box {
			display: flex;
			justify-content: space-evenly;

			.btn {
				margin-top: 20rpx;
				width: 200rpx;
				height: 80rpx;
				background-color: #F58519;
				border-radius: 50rpx;
				color: white;
				text-align: center;
				line-height: 80rpx;
				font-size: 28rpx;
			}
		}
	}
</style>